<template>
  <div id="questionView">
    <a-space style="margin-top: 1vh">
      <a-form :model="searchParams" layout="inline" class="search_input">
        <a-form-item field="title" label="题号">
          <a-input
            v-model="searchParams.questionId"
            placeholder="请输入你要查询的题号"
          />
        </a-form-item>
        <a-form-item field="language" label="语言">
          <a-select v-model="searchParams.language" style="width: 200px">
            <a-option>java</a-option>
            <a-option>cpp</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="userId" label="用户id">
          <a-input-tag v-model="searchParams.userId" style="min-width: 220px" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="doSubmit()">查询</a-button>
        </a-form-item>
      </a-form>
    </a-space>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total: total as number,
      }"
      @page-change="pageChange"
    >
      <template #questionTitle="{ record }">
        <a-space>
          <a-link
            @click="goQuestion(record.questionId)"
            :hoverable="true"
            class="question_link"
            >{{ record.questionTitle }}
          </a-link>
        </a-space>
      </template>
      <template #userId="{ record }">
        <a-space>
          {{ record.userName }}
        </a-space>
      </template>

      <template #status="{ record }">
        <a-space :color="colors[submitStatus[record.status]]">
          {{ submitStatus[record.status] }}
        </a-space>
      </template>

      <template #judgeInfo="{ record }">
        <a-space>
          {{ record.judgeInfo.message }}
        </a-space>
      </template>

      <template #code="{ record }">
        <a-space>
          <a-button @click="modelClick(record)" status="success"
            >点击查看</a-button
          >
        </a-space>
      </template>
      <template #submitNum="{ record }">
        <a-space> {{ record.submitNum }} </a-space>
      </template>
      <template #acceptNum="{ record }">
        <a-space> {{ record.acceptNum }} </a-space>
      </template>
      <template #createTime="{ record }">
        <a-space>
          {{ moment(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
        </a-space>
      </template>
      <template #operator="{ record }">
        <a-space>
          <a-button status="danger" @click="doDelete(record.id)">
            <template #default>删除</template>
            <template #icon>
              <icon-delete />
            </template>
          </a-button>
        </a-space>
      </template>
    </a-table>
    <a-modal
      draggable
      :modal-style="{
        width: 80,
        height: 80,
        minHeight: 800 + 'px',
        minWidth: 800 + 'px',
      }"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      :mask="true"
      okText="确定"
      cancelText="退出"
    >
      <template #title> {{ modal.language }}</template>
      <MdView :value="modal.code" />
      <CodeEditView
        :value="modal.code"
        :language="modal.language"
        :handleChange="handleChange"
      />
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionSubmitControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";
import { colors } from "@/access/GlobalColorEnum";
import { languages } from "monaco-editor";
import { submitStatus } from "../../access/SubmitStatus";
import CodeEdit from "@/components/CodeEdit.vue";
import * as vm from "vm";
import MdEdit from "@/components/MdEdit.vue";
import MdView from "@/components/MdView.vue";
import CodeEditView from "@/components/CodeEditView.vue";
import { IconDelete } from "@arco-design/web-vue/es/icon";

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "题目名称",
    slotName: "questionTitle",
  },
  {
    title: "提交用户",
    slotName: "userId",
  },
  {
    title: "语言",
    dataIndex: "language",
  },
  {
    title: "提交状态",
    slotName: "status",
  },
  {
    title: "提交信息",
    slotName: "judgeInfo",
  },
  {
    title: "代码",
    slotName: "code",
  },
  {
    title: "提交时间",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "operator",
  },
];

const searchParams = ref({
  current: 1,
  language: "",
  pageSize: 5,
  questionId: null,
  userId: null,
  sortField: "createTime",
  sortOrder: "descend",
} as QuestionSubmitQueryRequest);
const visible = ref(false);

const data = ref([]);
const total = ref(0);
const router = useRouter();
const modal = reactive({
  code:
    "import java.util.*;\n" +
    "\n" +
    "public class Main {\n" +
    "    public static void main(String[] args) {\n" +
    "        var sc = new Scanner(System.in);\n" +
    "        var a = Integer.parseInt(sc.next());\n" +
    "        var b = Integer.parseInt(sc.next());\n" +
    "        System.out.println(a+b);\n" +
    "    }\n" +
    "}",
  language: "",
});

// 模态框函数
const modelClick = (record: any) => {
  visible.value = true;
  modal.language = record.language;
  modal.code = record.code;
};
const handleChange = (v: string) => {
  console.log(v);
};
const handleOk = () => {
  visible.value = false;
};
const handleCancel = () => {
  visible.value = false;
};

const doDelete = async (id: number) => {
  const res = await QuestionSubmitControllerService.deleteQuestionSubmitById({
    id,
  });
  if (res.code === 0) {
    message.success("删除成功");
    data.value = data.value.filter((item, index) => {
      if (item.id === id) return false;
      return true;
    });
  } else {
    message.error("删除失败：" + res.message);
  }
};
// 函数
const loads = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    searchParams.value as QuestionQueryRequest
  );
  if (res.code === 0) {
    total.value = Number(res.data.total);
    res.data.records.forEach((item: any) => {
      item.userName = item.userVO.userName;
      item.code = "```" + "js" + "\n" + item.code + "\n" + "```";
      item.visible = false;
    });
    data.value = res.data.records;
  } else {
    message.error("数据出错" + res.message);
  }
};

onMounted(loads);

// 跳转到对应的题目页面
const goQuestion = (id: number) => {
  router.push({
    path: `/question/detail/${id}`,
  });
};

// 分页函数
const pageChange = (current: number) => {
  searchParams.value = {
    ...searchParams.value,
    current,
  };
  loads();
};
// 查询接口
const doSubmit = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loads();
};
</script>

<style scoped>
#questionView {
  min-width: 1200px;
  margin: auto;
  width: 80vw;
}
.question_link:hover {
  color: red;
  text-decoration: underline;
}
.search_input {
  padding: 8px;
}
.arco-modal {
  min-width: 600px;
}
</style>
